package com.iweb.controller;


import cn.hutool.jwt.JWT;
import com.iweb.commons.ResultEntity;
import com.iweb.model.Comment;
import com.iweb.model.Game;
import com.iweb.model.User;
import com.iweb.service.CommentService;
import com.iweb.service.GameService;
import com.iweb.service.UserCommentMotionService;
import com.iweb.service.UserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("comment")
public class CommentController {

    @Resource
    private CommentService commentService;
    @Resource
    private UserService userService;
    @Resource
    private UserCommentMotionService userCommentMotionService;
    @Resource
    private GameService gameService;
    //获取游戏评论
    @GetMapping("list_comment")
    public ResultEntity<List<Comment>> listGameComment(@RequestParam Integer page,@RequestParam("gameId") Integer gameId){
        List<Comment> comments = commentService.selectCommentByGameId(page,gameId);
        return new ResultEntity<>(2000,"列出游戏评论"+"页码"+page,true,comments);
    }

    //点赞,增加点赞数
    @PostMapping("great")
    public ResultEntity<List<Comment>> greatByCommentId(@RequestParam Integer commentId){
        //Integer userId = getUserId();
        Integer userId=1;
        Integer newMotion=1;
        Integer motion = userCommentMotionService.findMotion(userId,commentId);
        if (motion == null){
            commentService.greatCommentById(commentId);
            userCommentMotionService.newMotion(userId,commentId,newMotion);
            return new ResultEntity<>(2000,"点赞评论",true,null);
        }
        else if(motion.equals(0)){
            commentService.badMinusById(commentId);
            commentService.greatCommentById(commentId);
            userCommentMotionService.setNewMotion(userId, commentId,newMotion);
            return new ResultEntity<>(2000,"点赞评论",true,null);
        }else if (motion.equals(1)){
            return new ResultEntity<>(3001,"已经点赞过",false,null);
        }
        else {
            return new ResultEntity<>(3002,"未知错误",false,null);
        }
    }
    //点踩,增加点踩数
    @PostMapping("bad")
    public ResultEntity<Comment> badByCommentId(@RequestParam Integer commentId){
        //Integer userId = getUserId();
        Integer userId = 1;
        Integer newMotion=0;

        Integer motion = userCommentMotionService.findMotion(userId,commentId);
        if (motion == null){
            commentService.badCommentById(commentId);
            userCommentMotionService.newMotion(userId,commentId,newMotion);
            return new ResultEntity<>(2000,"点踩评论",true,null);
        }
        else if(motion.equals(1)){
            commentService.goodMinusById(commentId);
            commentService.badCommentById(commentId);
            userCommentMotionService.setNewMotion(userId, commentId,newMotion);
            return new ResultEntity<>(2000,"点踩评论",true,null);
        }else if (motion.equals(0)){
            return new ResultEntity<>(3001,"已经点踩过",false,null);
        }
        else {
            return new ResultEntity<>(3002,"未知错误",false,null);
        }
    }
    //发表评论
    @PostMapping("add")
    public ResultEntity<Comment> addCommnent(@RequestBody Comment comment){
        Integer userId = getUserId();
        User user = userService.findUserById(userId);
        Integer gameId = comment.getGameId();
        Game game = gameService.selectById(gameId);
        comment.setGameName(game.getGameName());
        comment.setUserId(userId);
        comment.setCommentBad(0);
        comment.setCommentGood(0);
        comment.setUserName(user.getUserName());
        comment.setCommentCreate(new Date());
        commentService.insertComment(comment);
        return new ResultEntity<>(2000,"发表评论",true,null);
    }
    //获取用户的评论, 从token中获取用户id
    @GetMapping("list_user")
    public ResultEntity<List<Comment>> listUserComment(){
        //Integer userId = getUserId();
        Integer userId = 1;
        List<Comment> comments = commentService.selectByUserId(userId);

        return new ResultEntity<>(2000,"ok",true,comments);
    }
    private Integer getUserId() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String token = request.getHeader("token");
        return Integer.parseInt(JWT.of(token).getPayload("userId").toString());
    }

    @PostMapping("delete")
    public ResultEntity<Comment> deleteComment(@RequestParam Integer commentId){
         commentService.deleteByCommentId(commentId);
        return new ResultEntity<>(2000,"ok",true,null);
    }

}
